package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.domain.OaRoleService;
import com.hundredapp.oa.domain.OaService;
import com.hundredapp.oa.repository.ServiceManager;
import com.hundredapp.oa.vo.ServiceVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Service1 extends ActionSupport{
	@Autowired
	private ServiceManager serviceManager;
	private List<ServiceVO> root;
	private int rsid;
	private int roleid;
	private int serviceid;
	private int permission;
	private Object success;
	private String msg;
	
	public String allservice() {
		root = new ArrayList<ServiceVO>();
		for(OaService serv:serviceManager.findAll()){
			ServiceVO temp=new ServiceVO();
			temp.setManagerName(serv.getManagerName());
			temp.setServiceText(serv.getServiceText());
			temp.setServiceId(serv.getId());
			if(roleid==0){
				temp.setPermission(0);
			}else{
				OaRoleService result=serviceManager.findRoleService(roleid, serv.getId());
				temp.setRoleId(roleid);
				temp.setRsid(result.getId());
				temp.setPermission(result.getPermission());
			}
			root.add(temp);
		}
		return SUCCESS;
	}
	
	public String updateservice() {
		OaRoleService roleService=new OaRoleService();
		roleService.setId(rsid==0?null:rsid);
		roleService.setRoleId(roleid);
		roleService.setServiceId(serviceid);
		roleService.setPermission(permission);
		serviceManager.saveOrUpdate(roleService);
		success = true;
		msg = "修改成功";
		return SUCCESS;
	}

	public List<ServiceVO> getRoot() {
		return root;
	}

	public void setServiceManager(ServiceManager serviceManager) {
		this.serviceManager = serviceManager;
	}

	public void setRoleid(int roleid) {
		this.roleid = roleid;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setServiceid(int serviceid) {
		this.serviceid = serviceid;
	}

	public void setPermission(int permission) {
		this.permission = permission;
	}

	public void setRsid(int rsid) {
		this.rsid = rsid;
	}
}
